package cn.anc.aonicardv.module.ui.album;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.tabs.TabLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.OnClick;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.R2;
import cn.anc.aonicardv.bean.FileBean;
import cn.anc.aonicardv.event.AllSelectEvent;
import cn.anc.aonicardv.event.EditEvent;
import cn.anc.aonicardv.event.HideBottomTabEvent;
import cn.anc.aonicardv.event.LocalDeleteEvent;
import cn.anc.aonicardv.event.LocalMoveEvent;
import cn.anc.aonicardv.event.LongClickEditEvent;
import cn.anc.aonicardv.event.SelectEvent;
import cn.anc.aonicardv.module.adpter.album.AlbumAdapter;
import cn.anc.aonicardv.module.ui.base.BaseFragment;
import cn.anc.aonicardv.net.download.DownLoadManager;
import cn.anc.aonicardv.util.DialogUtils;
import cn.anc.aonicardv.util.MediaScanner;
import cn.anc.aonicardv.widget.ControlSlideViewPager;
import cn.anc.aonicardv.widget.EditPopWindow;

/**
 * Created by yangdai on 2017/5/16.
 */

public class AlbumFragment extends BaseFragment implements TabLayout.OnTabSelectedListener, EditPopWindow.OnEditListener, ViewPager.OnPageChangeListener {

    private AlbumAdapter albumAdapter;
    @BindView(R2.id.tl_album)
    TabLayout albumTl;
    @BindView(R2.id.vp_album)
    ControlSlideViewPager albumVp;

    @BindView(R2.id.tv_right_text)
    TextView rightTv;
    @BindView(R2.id.tv_title)
    TextView titleTv;
    @BindView(R2.id.tv_left)
    TextView leftTv;
    @BindView(R2.id.tv_back)
    TextView backTv;
    private int count;
    private EditPopWindow editPopWindow;
    @BindView(R2.id.root_layout)
    LinearLayout rootLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return initView(R.layout.fragment_album);
    }

    @Override
    public void initVariable() {
        dialogUtils = new DialogUtils();
        albumAdapter = new AlbumAdapter(getFragmentManager(), getContext());
        editPopWindow = new EditPopWindow(getActivity().getWindow().getWindowManager().getDefaultDisplay().getWidth(), ViewPager.LayoutParams.WRAP_CONTENT, getContext());
    }


    @Override
    public void findViewById(View view) {
    }

    @Override
    public void setListener() {
        albumTl.addOnTabSelectedListener(this);
        editPopWindow.setOnEditListener(this);
        albumVp.addOnPageChangeListener(this);
    }

    @Override
    public void initData() {
        rightTv.setVisibility(View.VISIBLE);
        EventBus.getDefault().register(this);
        backTv.setVisibility(View.GONE);
        rightTv.setText(R.string.select);
        leftTv.setBackgroundColor(Color.TRANSPARENT);
        titleTv.setVisibility(View.GONE);
        albumVp.setAdapter(albumAdapter);
        albumTl.setupWithViewPager(albumVp);
        scanFile();
    }

    private void scanFile() {
        if (albumVp.getCurrentItem() == 0) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    MediaScanner.scanVideos(DownLoadManager.NORMAL_VIDEO_PATH, (MediaScanner.ScanCompleteCallBack) albumAdapter.getItem(0));
                }
            }).start();
        } else if (albumVp.getCurrentItem() == 1) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    MediaScanner.scanVideos(DownLoadManager.URGENT_VIDEO_PATH, (MediaScanner.ScanCompleteCallBack) albumAdapter.getItem(1));
                }
            }).start();
        } else if (albumVp.getCurrentItem() == 2) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    MediaScanner.scanImages(DownLoadManager.PHOTO_PATH, (MediaScanner.ScanCompleteCallBack) albumAdapter.getItem(2));
                }
            }).start();
        }


    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (!EventBus.getDefault().isRegistered(this))
                EventBus.getDefault().register(this);
            scanFile();
        } else
            EventBus.getDefault().unregister(this);
    }

    @OnClick(R2.id.tv_left)
    public void allSelect() {
        if (leftTv.getText().toString().equals(getString(R.string.all_select))) {
            count = getMediaNum();
            leftTv.setText(getString(R.string.cancel_all_select));
            EventBus.getDefault().post(new AllSelectEvent(true));
            editPopWindow.setDeleteEnable(true);
            editPopWindow.setShareEnable(true);
        } else {
            count = 0;
            leftTv.setText(getString(R.string.all_select));
            EventBus.getDefault().post(new AllSelectEvent(false));
            editPopWindow.setEnable(false);
        }
        titleTv.setText(getString(R.string.selected) + count);

    }

    private void goShare(String path, int shareType) {

    }

    private int getMediaNum() {
        int count = 0;
        if (albumVp.getCurrentItem() == 0) {
            VideoFragment p = (VideoFragment) albumAdapter.getItem(0);

            count = p.getVideoNum();

        } else if (albumVp.getCurrentItem() == 1) {
            UrgentFragment p = (UrgentFragment) albumAdapter.getItem(1);
            count = p.getVideoNum();
        } else if (albumVp.getCurrentItem() == 2) {
            PhotoFragment p = (PhotoFragment) albumAdapter.getItem(2);
            count = p.getPhotoNum();
        }
        return count;
    }


    private FileBean getSelectedFileBean() {
        FileBean fileBean = null;
        if (albumVp.getCurrentItem() == 0) {
            VideoFragment p = (VideoFragment) albumAdapter.getItem(0);
            fileBean = p.getSelectedPhotoBean();
        } else if (albumVp.getCurrentItem() == 1) {
            UrgentFragment p = (UrgentFragment) albumAdapter.getItem(1);
            fileBean = p.getSelectedPhotoBean();
        } else if (albumVp.getCurrentItem() == 2) {

            PhotoFragment p = (PhotoFragment) albumAdapter.getItem(2);
            fileBean = p.getSelectedPhotoBean();
        }
        return fileBean;
    }

    @OnClick(R2.id.tv_right_text)
    public void select() {
        count = getMediaNum();
        if (count == 0)
            return;
        if (rightTv.getText().toString().equals(getString(R.string.select))) {
            enterEdit();
        } else {
            exitEdit();
        }
    }

    private void enterEdit() {
        albumVp.setScroll(false);
        rightTv.setText(getString(R.string.exit));
        leftTv.setVisibility(View.VISIBLE);
        titleTv.setVisibility(View.VISIBLE);
        leftTv.setText(getString(R.string.all_select));
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) leftTv.getLayoutParams();
        layoutParams.leftMargin = 17;
        rightTv.setText(getString(R.string.exit));
        albumTl.setVisibility(View.GONE);
        EventBus.getDefault().post(new HideBottomTabEvent(true));
        editPopWindow.showAtLocation(rootLayout, Gravity.BOTTOM, 0, 0);
        EventBus.getDefault().post(new EditEvent(true));
        count = 0;
        titleTv.setText(getString(R.string.selected) + count);
    }

    private void exitEdit() {
        albumVp.setScroll(true);
        rightTv.setText(getString(R.string.select));
        editPopWindow.setEnable(false);
        leftTv.setVisibility(View.GONE);
        titleTv.setVisibility(View.GONE);
        albumTl.setVisibility(View.VISIBLE);
        EventBus.getDefault().post(new HideBottomTabEvent(false));
        editPopWindow.dismiss();
        EventBus.getDefault().post(new EditEvent(false));
        EventBus.getDefault().post(new AllSelectEvent(false));
        count = 0;
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        switch (tab.getPosition()) {
            case 0:
                albumVp.setCurrentItem(0);
                editPopWindow.setShareVisible(true);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        MediaScanner.scanVideos(DownLoadManager.NORMAL_VIDEO_PATH, (MediaScanner.ScanCompleteCallBack) albumAdapter.getItem(0));
                    }
                }).start();

                break;
            case 1:
                albumVp.setCurrentItem(1);
                editPopWindow.setShareVisible(false);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        MediaScanner.scanVideos(DownLoadManager.URGENT_VIDEO_PATH, (MediaScanner.ScanCompleteCallBack) albumAdapter.getItem(1));
                    }
                }).start();
                break;
            case 2:
                albumVp.setCurrentItem(2);
                editPopWindow.setShareVisible(false);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        MediaScanner.scanImages(DownLoadManager.PHOTO_PATH, (MediaScanner.ScanCompleteCallBack) albumAdapter.getItem(2));
                    }
                }).start();
                break;
        }

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    private DialogUtils dialogUtils;

    @Override
    public void delete() {
        if (count == 0) {
            Toast.makeText(getContext(), getString(R.string.select_hint), Toast.LENGTH_LONG).show();
            return;
        }

        dialogUtils.getCustomMessageAlertDialog(getActivity(), R.string.delete_hint).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                EventBus.getDefault().post(new LocalDeleteEvent());
                exitEdit();
                dialog.dismiss();
            }
        }).show();

    }

    @Override
    public void share() {
        if (albumVp.getCurrentItem() == 0) {
            //移动当前所选视频
            if (count == 0) {
                Toast.makeText(getContext(), getString(R.string.select_hint), Toast.LENGTH_LONG).show();
                return;
            }
            dialogUtils.getCustomMessageAlertDialog(getActivity(), R.string.move_hint).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            }).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    EventBus.getDefault().post(new LocalMoveEvent());
                    exitEdit();
                    dialog.dismiss();


                }
            }).show();


        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(LongClickEditEvent event) {
        select();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(SelectEvent event) {
        if (event.isSelect) {
            count++;
        } else
            count--;
        titleTv.setText(getString(R.string.selected) + count);
        if (count > 0) {
            editPopWindow.setDeleteEnable(true);
            editPopWindow.setShareEnable(true);

        } else {
            editPopWindow.setEnable(false);
        }
        if (count == getMediaNum())
            leftTv.setText(getString(R.string.cancel_all_select));
        else
            leftTv.setText(getString(R.string.all_select));
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (editPopWindow.isShowing()) {
                exitEdit();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

}
